package com.bdmc.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.core.domain.PageQuery;
import org.springframework.stereotype.Service;
import com.bdmc.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bdmc.common.utils.StringUtils;
import com.bdmc.base.domain.bo.BaseFencePointBo;
import com.bdmc.base.domain.vo.BaseFencePointVo;
import com.bdmc.base.domain.BaseFencePoint;
import com.bdmc.base.mapper.BaseFencePointMapper;
import com.bdmc.base.service.IBaseFencePointService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 围栏内检查点Service业务层处理
 *
 * @author bdmc
 * @date 2022-03-14
 */
@Service
public class BaseFencePointServiceImpl extends ServicePlusImpl<BaseFencePointMapper, BaseFencePoint, BaseFencePointVo> implements IBaseFencePointService {

    @Override
    public BaseFencePointVo queryById(Long pointId){
        return getVoById(pointId);
    }

    @Override
    public TableDataInfo<BaseFencePointVo> queryPageList(BaseFencePointBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaseFencePoint> lqw = buildQueryWrapper(bo);
        Page<BaseFencePointVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<BaseFencePointVo> mySelectPage(BaseFencePointBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.mySelectPage(pageQuery.build(),bo));
    }

    @Override
    public List<BaseFencePointVo> queryList(BaseFencePointBo bo) {
        LambdaQueryWrapper<BaseFencePoint> lqw = buildQueryWrapper(bo);
        return listVo(lqw);
    }

    private LambdaQueryWrapper<BaseFencePoint> buildQueryWrapper(BaseFencePointBo bo) {

        LambdaQueryWrapper<BaseFencePoint> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getFenceId() != null, BaseFencePoint::getFenceId, bo.getFenceId());
        lqw.like(StringUtils.isNotBlank(bo.getCheckName()), BaseFencePoint::getCheckName, bo.getCheckName());
        return lqw;
    }

    @Override
    public Boolean insertByBo(BaseFencePointBo bo) {
        BaseFencePoint add = BeanUtil.toBean(bo, BaseFencePoint.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setPointId(add.getPointId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(BaseFencePointBo bo) {
        BaseFencePoint update = BeanUtil.toBean(bo, BaseFencePoint.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(BaseFencePoint entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }

        return removeByIds(ids);
    }
}
